/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;


import java.util.Iterator;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.commands.AddUMLElementToDiagram;
import com.cea.papyrus.diagram.classdiagram.action.AddAllFeatures;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * this policy is use to apply resize of element
 */
public class AddAllFeaturesEditPolicy extends AbstractEditPolicy{

	public static final String ADD_ALL_FEATURES  = "ADD_ALL_FEATURES_POLICY";
	protected ClassEditPart getClassEditPart() {
		return (ClassEditPart)getHost();
	}
	/**
	 * used to obtain the graphNode that is associated to this editPolicy
	 * @return
	 */
	protected GraphNode getGraphNode() {
		return (GraphNode)getHost().getModel();
	}

/**
 * {@inheritDoc}
 */
	public Command getCommand(Request request) {
		if(AddAllFeatures.ADD_ALL_FEATURE_ACTION_REQUEST.equals(request.getType())){
			return getAddAllFeatureCommand(); 
		}
		return super.getCommand(request);
	}
	/**
	 * this method return a command to adapt the size of the figure 
	 * @return the command
	 */
	protected Command getAddAllFeatureCommand(){
		ClassEditPart classEditPart= getClassEditPart();
		CompoundCommand compoundCommand = new CompoundCommand();
		
		//iterate on properties
		Iterator<Property> iterp=classEditPart.getUmlElement().getAttributes().iterator();
		while(iterp.hasNext()){
			Property p=iterp.next();
			//can be display a port
			if(!(p instanceof Port)){
				AddUMLElementToDiagram c= new AddUMLElementToDiagram(
						p,
						(GraphNode)classEditPart.getGraphNode().getContained().get(0),
						new org.eclipse.draw2d.geometry.Point(0,0),
						new Dimension(0,0));
				compoundCommand.add(c);
			}
		}
		//iterate on operation to display it
		Iterator<Operation> itero=classEditPart.getUmlElement().getOperations().iterator();
		while(itero.hasNext()){
			Operation p=itero.next();
			AddUMLElementToDiagram c= new AddUMLElementToDiagram(
					p,
					(GraphNode)classEditPart.getGraphNode().getContained().get(1),
					new org.eclipse.draw2d.geometry.Point(0,0),
					new Dimension(0,0));
			compoundCommand.add(c);
		}

		return compoundCommand;
	}


}
